package br.usp.pcs.acs.negocios.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.usp.pcs.acs.infraestrutura.TipoRecurso;
import br.usp.pcs.acs.negocios.dao.interfaces.RecursoDaoInterface;
import br.usp.pcs.acs.negocios.dto.RecursoDto;


public class RecursoDao implements RecursoDaoInterface
{

	@Override
	public void edit(RecursoDto inDto)
	{		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_U);
			
			pstmt.setInt(7, inDto.getId());//id
			pstmt.setInt(1,inDto.getPedido_ca_id());
			pstmt.setInt(2,inDto.getPedido_id());
			pstmt.setInt(3,inDto.getPedido_ga_id());
			pstmt.setInt(4,inDto.getTipo().ordinal() + 1);
			pstmt.setInt(5,inDto.getPreco());
			pstmt.setInt(6,inDto.getQuantidadeDisp());
			
			pstmt.executeUpdate();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}

	@Override
	public ArrayList<RecursoDto> findAll() {
		return finding(CRUD_R);		
	}
	private ArrayList<RecursoDto> finding(String busca) {
		ArrayList<RecursoDto> LoutDto = new ArrayList<RecursoDto>();
		RecursoDto tmpDto = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
				
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(busca);			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{				
				tmpDto = new RecursoDto();
				tmpDto.setId(rs.getInt(1));
				tmpDto.setPedido_ca_id(rs.getInt(2));
				tmpDto.setPedido_id(rs.getInt(3));
				tmpDto.setPedido_ga_id(rs.getInt(4));				
				tmpDto.setTipo(TipoRecurso.valueOf(rs.getString(5)));
				tmpDto.setPreco(rs.getInt(6));
				tmpDto.setQuantidadeDisp(rs.getInt(7));
				
				System.out.println(tmpDto.getTipo());
				
				LoutDto.add(tmpDto);
			}
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
		
		return LoutDto;
	}

	@Override
	public RecursoDto findById(int id) {
		String busca =  "SELECT * FROM recurso WHERE id = " + id;
		ArrayList<RecursoDto> ret= finding(busca);
		return ret.isEmpty()?null:ret.get(0);
	}

	@Override
	public void insert(RecursoDto inDto)
	{		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_C);
			
			pstmt.setNull(1, java.sql.Types.INTEGER);//id
			pstmt.setInt(2,inDto.getPedido_ca_id());
			pstmt.setInt(3,inDto.getPedido_id());
			pstmt.setInt(4,inDto.getPedido_ga_id());
			pstmt.setInt(5,inDto.getTipo().ordinal() + 1);
			pstmt.setInt(6,inDto.getPreco());
			pstmt.setInt(7,inDto.getQuantidadeDisp());
			
			pstmt.execute();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	@Override
	public void remove(int id) 
	{
		Connection conn = null;
		PreparedStatement pstmt = null;		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(CRUD_D);			
			pstmt.setInt(1, id);			
			pstmt.execute();
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
}
